package com.dkh.recommend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkh.recommend.dto.CollectListDTO;
import com.dkh.recommend.dto.SaveCollectDTO;
import com.dkh.recommend.dto.Result;
import com.dkh.recommend.entity.TbCollect;
import com.dkh.recommend.entity.TbRelate;
import com.dkh.recommend.mapper.TbCollectMapper;
import com.dkh.recommend.mapper.TbMusicMapper;
import com.dkh.recommend.mapper.TbRelateMapper;
import com.dkh.recommend.service.ITbCollectService;
import com.dkh.recommend.utils.RecScoreUtils;
import com.dkh.recommend.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ITbCollectServiceImpl extends ServiceImpl<TbCollectMapper, TbCollect> implements ITbCollectService {

    @Autowired
    private TbCollectMapper collectMapper;

    @Autowired
    private TbRelateMapper relateMapper;

    @Autowired
    private TbMusicMapper musicMapper;

    @Override
    public Result GetCollectMusicIds(Long collectId) {

//        根据收藏加快ID获取收藏夹列表
        LambdaQueryWrapper<TbCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbCollect::getCollectId,collectId);
        List<TbCollect> collectList = this.list(queryWrapper);
//        通过流遍历收藏夹列表获取音乐ids
//        创建音乐list
        List<Long> musicIds = new ArrayList<>();
        collectList.stream().forEach(
                collect -> musicIds.add(collect.getMusicId())
        );
        return Result.ok(musicIds);
    }

    /**
     * 将音乐添加到收藏夹中
     * @param collect
     * @return
     */
    @Transactional
    @Override
    public Result AddMusic2Collect(TbCollect collect) {

        Long userId = UserHolder.getUser().getUserId();
        LambdaQueryWrapper<TbCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbCollect::getCollectId,collect.getCollectId())
                .eq(TbCollect::getMusicId,collect.getMusicId());
        List<TbCollect> list = this.list(queryWrapper);
        if (list.size() > 0){
            return Result.fail("歌曲已经在该收藏夹");
        }
        boolean b = this.save(collect);
        if (!b){
            return Result.fail("收藏失败！！");
        }

//        收藏数+1
        musicMapper.addCountCollect(collect.getMusicId());

        //            收藏+1分
        TbRelate relate = new TbRelate();
        relate.setMusicId(collect.getMusicId());
        relate.setUserId(userId);
        relate.setIndex(1);
        RecScoreUtils.addScore(relate,relateMapper);
        return Result.ok();
    }

    /**
     * 删除收藏夹内的音乐
     * @param collect
     * @return
     */
    @Override
    public Result DeleteMusic2Collect(TbCollect collect) {

        Long userId = UserHolder.getUser().getUserId();
        LambdaQueryWrapper<TbCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbCollect::getCollectId,collect.getCollectId())
                .eq(TbCollect::getMusicId,collect.getMusicId());
        boolean b = this.remove(queryWrapper);
        if (!b){
            return Result.fail("删除失败！！");
        }

        //            取消收藏-1分
        TbRelate relate = new TbRelate();
        relate.setMusicId(collect.getMusicId());
        relate.setUserId(userId);
        relate.setIndex(1);
        RecScoreUtils.decScore(relate,relateMapper);

        return Result.ok();
    }

    /**
     * 查询收藏夹音乐总数
     * @param collectId
     * @return
     */
    @Override
    public Result countMusic2Collect(String collectId) {

        Long collectIdL = Long.valueOf(collectId);

        LambdaQueryWrapper<TbCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbCollect::getCollectId,collectIdL);
        int countMusic = this.count(wrapper);
        return Result.ok(countMusic);
    }


    /**
     * 添加音乐到多个收藏夹中
     * @param saveCollectDTO
     * @return
     */
    @Override
    public Result Add2Music2Collect(SaveCollectDTO saveCollectDTO) {
        Long musicId = saveCollectDTO.getMusicId();
        List<Long> collectIdS = saveCollectDTO.getCollectIdS();
        List<TbCollect> collectList = new ArrayList<>();
        collectIdS.stream().forEach(collectId->{
            TbCollect tbCollect = new TbCollect();
            tbCollect.setMusicId(musicId);
            tbCollect.setCollectId(collectId);
        });
//        将音乐保存到多个收藏夹内
        boolean b = this.saveBatch(collectList);
        if (!b){
            return Result.fail("保存失败");
        }

        return Result.ok();
    }


    /**
     * 根据收藏夹id获取收藏夹内的音乐列表
     * @param collectId
     * @return
     */
    @Override
    public Result getMyCollectMsicuListById(String collectId) {
//         getMyCollectListById
        Long collectIdL = Long.valueOf(collectId);
        List<CollectListDTO> myCollectListById = collectMapper.getMyCollectListById(collectIdL);
        return Result.ok(myCollectListById);
    }
}
